<template>
  <div class="verifyProcessDetail h100">
    <div class="verifyProcessDetailTop">
      <!-- 审核流程 -->
      <review-process
        @transTime="handleEndTime"
        @showInfo="handlerShowInfoBtn"
        @hideAddInfoBtn="handleAddInfodrawBtn"
        @addInfoBtn="addInfodrawBtn"
        @hideWithdrawBtn="handleHideWithdrawBtn"
        @transFileLimit="handleFileLimit"
        v-if="BottomType!==''"
        :isPendingReview="BottomType"
      />
      <!-- 审核意见 -->
      <review-process-action
        ref="actionCompt"
        id="reviewAction"
        v-if="BottomType==='pendingReview'"
        :fileLimitObj="fileLimitObj">
      </review-process-action>
    </div>
    <div class="taskVerifyDetailFooter" >
      <!-- 审核操作/结果 -->
      <div>
        <a-row>
          <a-col :md="2" :sm="2"></a-col>
          <a-col :md="18" :sm="18">
            <div v-if="BottomType==='passedReview'">
              <span class="marginRightText ">{{ $t('auditResult') }}</span>
              <span class="marginRightTextBigger ok titleBold">{{ $t('passed') }}</span>
              <span class="marginRightText ">{{ $t('completeTime') }}</span><span class="titleBold">{{ endTime }}</span>
<!--              <span class="marginRightText "><a-button type="primary">{{ $t('auditDetailView') }}</a-button></span>-->
            </div>
            <div v-if="BottomType==='pendingApplication'">
              <auth-button type="primary" style="margin-right: 24px" @click="urge">{{ $t('urge') }}</auth-button>
              <auth-button style="margin-right: 24px" v-if="!hideWithdrawBtn" @click="revoke">{{ $t('withdraw') }}</auth-button >
            </div>
            <div v-if="BottomType==='rejectedReview'">
              <span class="marginRightText ">{{ $t('auditResult') }}</span>
              <span class="marginRightTextBigger reject titleBold">{{ $t('rejected') }}</span>
              <span class="marginRightText ">{{ $t('completeTime') }}</span><span class="titleBold">{{ endTime }}</span>
            </div>
            <div v-if="BottomType==='canceledReview'">
              <span class="marginRightTextBigger titleBold">{{ $t('sqych') }}</span>
              <span class="marginRightText ">{{ $t('timeofCancel') }}</span>
              <span class="titleBold">{{ endTime }}</span>
            </div>
            <div v-if="BottomType==='pendingReview'">
              <auth-button type="primary" style="margin-right: 24px;background:#ddd;color:#fff;border:none" @click="pass"
              v-if="$route.query.swcode === 'task_audit' && passDisabled && (taskName.includes(role.qualityEngineer)
                                            ||
                                            taskName.includes(role.OTAEngineer)
                                            ||
                                            taskName.includes(role.testEngineer))">
                {{ $t('pass') }}
              </auth-button >
              <auth-button  type="primary" :disabled="addInfoBtn" style="margin-right: 24px" @click="pass" v-else-if="$route.query.swcode === 'sw_repo_audit'">
              {{ $t('pass') }}
            </auth-button >
              <auth-button type="primary" style="margin-right: 24px" @click="pass" v-else>
                {{ $t('pass') }}
              </auth-button >
              <auth-button @click="reject" style="margin-right: 24px" >
                {{ $t('reject') }}
              </auth-button >
              <auth-button v-if="hideAddInfoBtn || showInfo" @click="addReviewInfo" style="margin-right: 24px" >
                {{ $t('addAuditInformation') }}
              </auth-button >
            </div>
          </a-col>
        </a-row>
      </div>
    </div>
      <!-- 新增对话框 -->
    <add-audit-info
      ref="addConfigModal"
      @addOrEditDone="handleRefreshTable"
    ></add-audit-info>
  </div>
</template>
<script>
import AddAuditInfo from '@/views/todo/myApplication/components/AddAuditInfo.vue'
import QuestionMarkTip from '@/views/components/QuestionMarkTip'
import TableEllipsis from '@/components/Ellipsis/TableEllipsis'
import ReviewProcess from '@/views/todo/myApplication/components/ReviewProcessCompt'
import ReviewProcessAction from '@/views/todo/myReview/components/ReviewProcessActionCompt'
import { reviewProcessColumns } from './config/list'
import AuthButton from '@/components/AuthButton'
import { minxinDrawerUse } from '@/utils/mixin'
import OTAReviewDetail from '../myReview/components/OTAReviewDetail.vue'
import QualityReviewDetail from '../myReview/components/QualityReviewDetail.vue'
import TestReviewDetail from '../myReview/components/TestReviewDetail.vue'
import myApplyApi from "@/api/modules/todo/myApply"
import {OTAEngineer,qualityEngineer,testEngineer} from "@/views/todo/myReview/config/nodeNameList.js"
export default {
  mixins: [minxinDrawerUse],
  components: { TableEllipsis, QuestionMarkTip, ReviewProcess, AuthButton, ReviewProcessAction, AddAuditInfo },
  data () {
    return {
      passDisabled:false,
      showInfo:false,
      // 审核层级
      taskName:'',
      role:{
        OTAEngineer,
        qualityEngineer,
        testEngineer
      },
      formLayout: 'horizontal',
      showEdit: false,
      hideWithdrawBtn: false,
      addInfoBtn: false,
      hideAddInfoBtn: false,
      columns: reviewProcessColumns,
      endTime: '',
      BottomType: '',
      fileLimitObj: {
        fileCount: 1,
        fileSize: 1
      }
    }
  },
  computed: {
  },
  watch: {
  },
  methods: {
    initQuery (val) {
      if (val.aorr === 'a') {
        // 从我的申请页面跳转过来的
        if (val.status === '10' || val.status === '0') {
          this.BottomType = 'pendingApplication'
        } else if (val.status === '20') {
          this.BottomType = 'passedReview'
        } else if (val.status === '30') {
          this.BottomType = 'rejectedReview'
        } else if (val.status === '40') {
          this.BottomType = 'canceledReview'
        }
      } else {
        // 从我的审核页面跳转过来的
        if (val.status === '10' || val.status === '0') {
          this.BottomType = 'pendingReview'
        } else if (val.status === '20') {
          this.BottomType = 'passedReview'
        } else if (val.status === '30') {
          this.BottomType = 'rejectedReview'
        } else if (val.status === '40') {
          this.BottomType = 'canceledReview'
        }
      }
      // 如果是点击通过或驳回，需要跳转到页面对应锚点
      if (val.toAction) {
        this.$nextTick(() => {
          document.getElementById('reviewAction').scrollIntoView()
        })
      }
    },
     // 新增或编辑配置成功以后刷新表格
    handleRefreshTable () {
      this.addInfoBtn=false
      // this.$refs.flowConfigTableComp.refreshData(true)
    },
    // 后端控制上传文件的限制
    handleFileLimit (data) {
      this.fileLimitObj = data
    },
     // 控制添加补充信息按钮是否显示
    handleAddInfodrawBtn (bool) {
      this.hideAddInfoBtn = bool
    },
     // 控制通过按钮是否可用
    addInfodrawBtn (bool) {
      this.addInfoBtn = bool
    },
    // 控制撤回按钮是否显示
    handleHideWithdrawBtn (bool) {
      this.hideWithdrawBtn = bool
    },
    handlerShowInfoBtn(bool){
      this.showInfo=bool
    },
    // 完成时间
    handleEndTime (data) {
      this.endTime = this.$options.filters.moment(data)
    },
    // 催办
    urge () {
      const vm = this
      this.$confirm({
        title: vm.$t('systemTip'),
        content: vm.$t('urgeApplyTip'),
        onOk () {
          vm.$api.urgeMyApply({ instanceId: vm.$route.query.id }).then((res) => {
            if (res.message === 'success') {
              vm.$message.success(vm.$t('5089'))
              vm.$store.dispatch('tagsView/delView', vm.$route.fullPath)
                .then(vm.$router.push({ path: '/todo/myapplication' }))
            }
          })
        },
        onCancel () { }
      })
    },
    // 撤回
    revoke () {
      const vm = this
      this.$confirm({
        title: vm.$t('systemTip'),
        content: vm.$t('withdrawApplyTip'),
        onOk () {
          vm.$api.cancelMyApply({ instanceId: vm.$route.query.id }).then((res) => {
            if (res.message === 'success') {
              vm.hideWithdrawBtn = true
              vm.$message.success(vm.$t('successfullyOperation'))
              vm.$store.dispatch('tagsView/delView', vm.$route.fullPath)
                .then(vm.$router.push({ path: '/todo/myapplication' }))
            }
          })
        },
        onCancel () { }
      })
    },
    // 驳回审核
    reject () {
      const vm = this
      if (this.$refs.actionCompt.formData.taskComment === '') {
        this.$message.warning('审批意见不能为空')
      } else {
        this.$api.rejectThisAudit(this.$refs.actionCompt.formData).then((res) => {
          if (res.message === 'success') {
            vm.$store.dispatch('tagsView/delView', vm.$route.fullPath)
              .then(vm.$router.push({ path: '/todo/myReview' }))
          }
        })
      }
    },
    passToMyReview(){
      const vm = this
      vm.$store.dispatch('tagsView/delView', vm.$route.fullPath)
      .then(vm.$router.push({ path: '/todo/myReview' }))
    },
    // 通过审核
    pass () {
      const vm = this
      if(this.passDisabled && this.$route.query.swcode === 'task_audit'){
        this.$message.warning('请补充待审核信息')
      }
      else if (this.$refs.actionCompt.formData.taskComment === '') {
        this.$message.warning('审批意见不能为空')
      }
      else {
        if(this.taskName.includes(OTAEngineer) && this.$route.query.swcode === 'task_audit'){
          myApplyApi.uploadOTAReviewDetail(this.ReviewDetail).then(res=>{
            if (res.message === 'success') {
              this.passToMyReview()
            }
          })
        }
        else if(this.taskName.includes(testEngineer) && this.$route.query.swcode === 'task_audit'){
          myApplyApi.uploadTestReviewDetail(this.ReviewDetail).then(res=>{
            if (res.message === 'success') {
              this.passToMyReview()
            }
          })
        }
        else if(this.taskName.includes(qualityEngineer) && this.$route.query.swcode === 'task_audit'){
          myApplyApi.uploadQualityReviewDetail(this.ReviewDetail).then(res=>{
            if (res.message === 'success') {
              this.passToMyReview()
            }
          })
        }
        else{
          this.$api.passThisAudit(vm.$refs.actionCompt.formData).then((res) => {
            if (res.message === 'success') {
              this.passToMyReview()
            }
          })
        }
      }
    },
    getTaskName(){
      this.$api.getTaskReviewDetail({
        id:this.$route.query.p
      }).then((res)=>{
        this.taskName=res.data.taskName
        if( this.$route.query.swcode === 'task_audit' &&
          (this.taskName.includes(qualityEngineer)
          ||
          this.taskName.includes(OTAEngineer)
          ||
          this.taskName.includes(testEngineer))){
            this.passDisabled=true
          }
      })
    },
    saveReviewInfo (data) {
      this.passDisabled=false
      this.ReviewDetail=data
      this.toggleDrawerMain(false)
    },
    // 补充审核信息
    async addReviewInfo(){
      if(this.$route.query.swcode === 'sw_repo_audit'){
        this.$refs.addConfigModal.switchVisible(true, 'add', null)
      }
      if(this.$route.query.swcode === 'task_audit'){
        const buttons = this.getRightDrawerFooterButtons({
          method: 'saveReviewInfo'
        })
        if(this.taskName.includes(qualityEngineer)){
          this.$api.getTaskInfor({
            id:this.$route.query.key
          }).then((response)=>{
            this.showRightDrawerNoReset( QualityReviewDetail, this.taskName, buttons, 'large', {
              upgradeTriggerType:this.upgradeTriggerType,
              auditInfo:this.$refs.actionCompt.formData,
              strategyId:response.data.strategyId
            })
          })
        }else if(this.taskName.includes(OTAEngineer)){
          this.showRightDrawerNoReset(OTAReviewDetail, this.taskName, buttons, 'small', {
            auditInfo:this.$refs.actionCompt.formData
          })
        }else if(this.taskName.includes(testEngineer)){
          this.showRightDrawerNoReset(TestReviewDetail, this.taskName, buttons, 'small', {
            auditInfo:this.$refs.actionCompt.formData
          })
        }
      }
    },
  },
  created(){
    this.initQuery(this.$route.query);
    this.getTaskName()
  }
}
</script>
<style lang="less" scoped>
.verifyProcessDetail {
  display: flex;
  flex-direction: column;
  .verifyProcessDetailTop {
    position: relative;
    flex: 1;
    overflow: auto;
    padding: 15px 24px;
    min-width: 800px;
  }
  .verifyProcessDetailFooter {
    padding: 15px 24px;
    flex: none;
    border-top: 1px solid @borderColor;
    .ok {
      color: #46d5a2;
      font-weight: bold;
    }

    .reject {
      color: #fe8b67;
      font-weight: bold;
    }
  }
  .taskVerifyDetailFooter {
    font-size: 18px;
    padding: 15px 24px;
    flex: none;
    border-top: 1px solid @borderColor;
    .ok {
      color: #46d5a2;
      font-weight: bold;
    }
    .reject {
      color: #fe8b67;
      font-weight: bold;
    }
    .marginRightText{
      margin-right: 36px;
    }
    .marginRightTextBigger{
      margin-right: 66px;
    }
    .titleBold{
      font-weight: 600;
    }
  }
}
</style>
